﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace 软件发布平台.ViewModel
{
    public class User : Notify
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged("Name"); }
        }
        private bool check = false;

        public bool Check
        {
            get { return check; }
            set { check = value; RaisePropertyChanged("Check"); }
        }
    }
    public class ReleaseViewModel : Notify
    {
        public ReleaseViewModel()
        {
        }
        private List<CheckBoxTreeViewModel> _Items = new List<CheckBoxTreeViewModel>() { };
        private int _BarValue = 0;
        private string _BarText = "0%";
        private Visibility _Visible = Visibility.Visible;
        public List<CheckBoxTreeViewModel> Items
        {
            get => _Items;
            set { _Items = value; RaisePropertyChanged("Items"); }
        }
        public int BarValue { get => _BarValue;set { _BarValue = value;RaisePropertyChanged("BarValue"); } }
        public string BarText { get => _BarText; set { _BarText = value; RaisePropertyChanged("BarText"); } }
        public Visibility Visible { get => _Visible; set { _Visible = value; RaisePropertyChanged("Visible"); } }
        private string guDingLuJing;
        public string GuDingLuJing
        {
            get { return guDingLuJing; }
            set { guDingLuJing = value; RaisePropertyChanged("GuDingLuJing"); }
        }
        private bool isGuDingLuJing = false;

        public bool IsGuDingLuJing
        {
            get { return isGuDingLuJing; }
            set { isGuDingLuJing = value; RaisePropertyChanged("IsGuDingLuJing"); }
        }
        private string fbUser;

        public string FbUser
        {
            get { return fbUser; }
            set { fbUser = value; RaisePropertyChanged("FbUser"); }
        }
        private string spUser;

        public string SpUser
        {
            get { return spUser; }
            set { spUser = value; RaisePropertyChanged("SpUser"); }
        }
        private string csUser;

        public string CsUser
        {
            get { return csUser; }
            set { csUser = value; RaisePropertyChanged("CsUser"); }
        }
        private string temp;

        public string Temp
        {
            get { return temp; }
            set { temp = value; RaisePropertyChanged("Temp"); }
        }
        private List<string> releaseUsers;

        public List<string> ReleaseUsers
        {
            get { return releaseUsers; }
            set { releaseUsers = value; RaisePropertyChanged("ReleaseUsers"); }
        }
        private List<string> checkUsers;

        public List<string> CheckUsers
        {
            get { return checkUsers; }
            set { checkUsers = value; RaisePropertyChanged("CheckUsers"); }
        }
        private List<string> temps;

        public List<string> Temps
        {
            get { return temps; }
            set { temps = value; RaisePropertyChanged("Temps"); }
        }
        private ObservableCollection<User> pushUsers;

        public ObservableCollection<User> PushUsers
        {
            get { return pushUsers; }
            set { pushUsers = value; RaisePropertyChanged("PushUsers"); }
        }
        private bool isShenPi=true;

        public bool IsShenPi
        {
            get { return isShenPi; }
            set { isShenPi = value; RaisePropertyChanged("IsShenPi"); }
        }
        private bool isTuiSong=true;

        public bool IsTuiSong
        {
            get { return isTuiSong; }
            set { isTuiSong = value; RaisePropertyChanged("IsTuiSong"); }
        }

        //public void InitTree(string path,ref List<FileInfo> files, List<CheckBoxTreeViewModel> treeViewModels)
        //{
        //    DirectoryInfo dir = new DirectoryInfo(path);
        //    FileInfo[] allFile = dir.GetFiles();
        //    foreach (FileInfo fi in allFile)
        //    {
        //        if (fi.Name.Contains(".pdb"))
        //        {
        //            continue;
        //        }
        //        files.Add(fi);
        //        CheckBoxTreeViewModel tnode = new CheckBoxTreeViewModel();
        //        tnode.Header = fi.Name;
        //        tnode.Path = fi.FullName;
        //        tnode.Icon = PackIconKind.File;
        //        treeViewModels.Add(tnode);
        //    }
        //    DirectoryInfo[] allDir = dir.GetDirectories();
        //    foreach (DirectoryInfo d in allDir)
        //    {
        //        CheckBoxTreeViewModel tnode = new CheckBoxTreeViewModel();
        //        tnode.Header = d.Name;
        //        tnode.Path = d.FullName;
        //        treeViewModels.Add(tnode);
        //        InitTree(d.FullName, ref files, tnode.Children);
        //    }
        //}
        public void InitTree(string path,List<FileInfo> files, List<CheckBoxTreeViewModel> treeViewModels, List<string> removeFiles, List<string> configFiles = null, string exePath="", CheckBoxTreeViewModel parent=null)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] allFile = dir.GetFiles();

            foreach (FileInfo fi in allFile)
            {
                //if (fi.Name.Contains(".pdb"))
                //{
                //    continue;
                //}
                if (removeFiles.Contains(fi.FullName))
                {
                    continue;
                }
                files.Add(fi);
                CheckBoxTreeViewModel tnode = new CheckBoxTreeViewModel();
                tnode.Header = fi.Name;
                tnode.Path = fi.FullName;
                tnode.Parent = parent;
                tnode.Icon = PackIconKind.File;
                if (configFiles != null && configFiles.Contains(fi.FullName))
                {
                    tnode.IsChecked = true;
                }
                if (exePath.Equals(fi.FullName))
                {
                    tnode.ForeColor = new SolidColorBrush(Color.FromRgb(50, 200, 50));
                }
                treeViewModels.Add(tnode);
            }

            DirectoryInfo[] allDir = dir.GetDirectories();
            foreach (DirectoryInfo d in allDir)
            {
                if (removeFiles.Contains(d.FullName))
                {
                    continue;
                }     
                CheckBoxTreeViewModel tnode = new CheckBoxTreeViewModel();
                tnode.Header = d.Name;
                tnode.Path = d.FullName;
                tnode.Children = new List<CheckBoxTreeViewModel>();
                treeViewModels.Add(tnode);
                InitTree(d.FullName,files, tnode.Children, removeFiles,configFiles,exePath, tnode);
            }
        }
    }
    public class CheckBoxTreeViewModel : Notify
    {
        private bool? _IsChecked = false;
        private bool _IsThree = false;
        private string _Path;
        private string _Header = string.Empty;
        private SolidColorBrush _ForeColor = new SolidColorBrush(Color.FromRgb(0, 0, 0));
        private SolidColorBrush _BackColor = new SolidColorBrush(Color.FromRgb(255, 250, 250));
        private PackIconKind _Icon = PackIconKind.Folder;
        private List<CheckBoxTreeViewModel> _Children = new List<CheckBoxTreeViewModel>();
        private CheckBoxTreeViewModel _Parent = null;
        public bool IsThree { get => _IsThree; set { _IsThree = value; } }
        public bool? IsChecked { get => _IsChecked; set { this.SetIsChecked(value, true, true); } }
        public string Header { get => _Header; set => _Header = value; }
        public SolidColorBrush BackColor { get => _BackColor; set { _BackColor = value; this.RaisePropertyChanged("BackColor"); } }
        public SolidColorBrush ForeColor { get => _ForeColor; set { _ForeColor = value; this.RaisePropertyChanged("ForeColor"); } }
        public List<CheckBoxTreeViewModel> Children
        { 
            get => _Children; 
            set { _Children = value; SetParentValue(); } 
        }
        public string Path { get => _Path; set => _Path = value; }
        public PackIconKind Icon { get => _Icon; set => _Icon = value; }
        public CheckBoxTreeViewModel Parent { get => _Parent; set => _Parent = value; }

        /// <summary>
        /// 设置节点IsChecked的值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="updateChildren"></param>
        /// <param name="updateParent"></param>
        private void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
        {
            if (value == _IsChecked)
                return;
            _IsChecked = value;
            if (updateChildren && _IsChecked.HasValue && Children != null)
            {
                this.Children.ForEach(c => c.SetIsChecked(_IsChecked, true, false));
            }
            if (updateParent && Parent != null)
            {
                Parent.VerifyCheckState();
            }
            this.RaisePropertyChanged("IsChecked");
        }
        /// <summary>
        /// 验证并设置父级节点的IsChecked的值
        /// </summary>
        private void VerifyCheckState()
        {
            bool? state = null;
            for (int i = 0; i < this.Children.Count; ++i)
            {
                bool? current = this.Children[i].IsChecked;
                if (i == 0)
                {
                    state = current;
                }
                else if (state != current)
                {
                    state = null;
                    break;
                }
            }
            this.IsChecked = state;
        }
        /// <summary>
        /// 数据初始化时设置父节点的值
        /// </summary>
        private void SetParentValue()
        {
            if (this.Children != null)
            {
                this.IsThree = true;
                this.Children.ForEach(ch => ch.Parent = this);
            }
        }
    }
    public class Notify : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void RaisePropertyChanged(string propertyname)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}
